package pack0821;


import java.util.Scanner;

public class DemoA {
    Scanner in = new Scanner(System.in);
    public void a (){
        try{
            int[] arr = {2,3,4,5,6};
            System.out.println("请输入索引号：");
            int index = in.nextInt();
            System.out.println("查询内容为"+arr[index]);
        }catch(Exception e){
            System.out.println("输入范围0-4");
            System.out.println(e.getMessage());//显示错误代码
            e.printStackTrace();//显示堆栈错误
        }
        System.out.println("我是异常处理之外的代码");
    }
    public void b(){
        try{
            int[] arr = {2,3,4,5,6};
            System.out.println("请输入索引号：");
            int index = in.nextInt();
            System.out.println("查询内容为"+arr[index]);
        }catch (ArithmeticException e){
            System.out.println("输入范围0-4");
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        System.out.println("我是异常处理之外的代码");
    }
    public void c(){
        try{
            String s = "asdfgh";
            System.out.println("请输入索引号");
            int index = in.nextInt();
            System.out.println("查询内容为"+s.charAt(index));
        }catch(Exception e){
            System.out.println("输入范围0-4");
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        System.out.println("我是异常处理之外的代码");
    }

    public static void main(String[] args) {
        new DemoA().c();
    }
}
